package com.txl.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.txl.common.BaseException;
import com.txl.constants.Constants;
import com.txl.mapper.TbUserMapper;
import com.txl.pojo.TbUser;
import com.txl.service.TbUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

import static java.lang.Math.max;

import java.util.stream.Collectors;

@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements TbUserService {

    @Resource
    private TbUserMapper tbUserMapper;

    @Override
    public TbUser getTbUserList(TbUser tbUser) {
        int max = max(1, 3);
        Math.max(1, 3);
        TbUser user = tbUserMapper.selectById(tbUser);
        if (StringUtils.isEmpty(user)) {
            throw new BaseException("用户为空");
        }
        return user;
    }

    @Override
    @Transactional
    public List<TbUser> getUserList() {
//        List<TbUser> list = list(new QueryWrapper<TbUser>().eq("user_name", "张阳君").eq("id", 1));
//        List<TbUser> list = list(new QueryWrapper<TbUser>().eq("user_name", "张阳君"));
//        System.out.println(list);
//        return list;
//        return this.lambdaQuery().like(TbUser::getUserName, "阳").list().stream().collect(Collectors.toList());
//        this.getOne(new QueryWrapper<>().eq("user_name","张阳君"));
//        QueryWrapper<TbUser> query = Wrappers.query();
//        query.eq("user_name","张阳君");
//        query.last("limit 1");
//        this.getOne(query);

//        System.out.println(this.getOne(this.lambdaQuery().eq(TbUser::getUserName, "张阳君")));
//        this.lambdaUpdate().eq(TbUser::getId,1).set(TbUser::getUserName,"张阳君2");
        UpdateWrapper<TbUser> updateWrapper = new UpdateWrapper<TbUser>().eq("id", 1).set("user_name", "张阳君8888888888");
        this.tbUserMapper.update(null, updateWrapper);
        System.out.println(this.lambdaQuery().like(TbUser::getUserName, "阳").list().stream().filter(item -> item.getId() > 5).collect(Collectors.toList()).get(0));
        return this.lambdaQuery().like(TbUser::getUserName, "阳").list();
    }

    @Override
    public Page<TbUser> queryUsers(int pageNum, int pageSize) {
        return tbUserMapper.queryUsers(new Page<>(pageNum, pageSize));
    }

    @Override
    public List<String> getUserListByStream() {
        Constants.CAPTCHA_EXPIRATION = 32;
        List<TbUser> userList = tbUserMapper.selectUserList();
        List<String> list = userList.stream().map(TbUser::getId).filter(id -> id < 5)
                .map(id -> tbUserMapper.selectById(id)).collect(Collectors.toList())
                .stream().map(TbUser::getUserName).collect(Collectors.toList());
        return list;
    }

    @Override
    public Object testStatic() {
        Constants constants = new Constants();
        String notStatic = constants.notStatic;
        return Constants.CAPTCHA_EXPIRATION;
    }
}
